#===============================================================================
# Koro Business Environment
# Copyright (C) 2008  Antonio Corroppoli
#
#This file is part of Koro BE.
# 
# Koro BE is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Koro BE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Koro BE; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#===============================================================================

'''
Created on 11/ago/2011

@author: antstud
'''

import wx
from koro.ui.wxgui.pages import BasePage
from koro.ui.wxgui.widgets import controls as co
from koro.ui.wxgui.mellcfg import mDefaultBorder
from koro.modules.anagrafiche.ui import panel
from koro.modules.commerciale.ui.panel import DatiCommercialiPanel

#===============================================================================
# AnagraficaBasePage
#===============================================================================
class AnagraficaBasePage(BasePage):
    TITLE=''
    def __init__(self,*args,**kwargs):        
        BasePage.__init__(self,*args,**kwargs)        
        
        self.notebook = co.KoroNotebook(self)
        
        self.__do_layout(args[2])        
        self.__bind()
        self.SetAutoLayout(True)
        #self.init_page_view()
    
    def __do_layout(self,objects):
        self.fieldsizer = fieldsizer = wx.GridBagSizer(0,0)                
        
        self.init_notebook()
        self.init_page_list()
        
        self.load_list_objects(objects)
        
        fieldsizer.Add(self.notebook,(0,0),(1,1),flag=wx.ALL|wx.EXPAND ,border=3)#riga zero colonna 0
        fieldsizer.Add(self.list_manager.get_managed_list(),(0,1),flag=wx.ALL|wx.EXPAND ,border=3)#riga 0 colonna 1
        fieldsizer.AddGrowableCol(1)
        #fieldsizer.AddGrowableCol(0)
        fieldsizer.AddGrowableRow(0)        
        
        self.Sizer.Add(fieldsizer,1,flag=wx.ALL|wx.EXPAND ,border=0)
        #self.SetSizerAndFit(topsizer)
    
    def __bind(self):
        pass
    
    def enable_page(self,enable=True):
        self.notebook.Enable(enable)
    
    def update_fields(self,dataObj):
        print self.__class__.__name__,'.update_fields'        
        self.anagrafica.update_fields(dataObj)
        self.dati_commerciali.update_fields(dataObj)

    def update_object(self,obj):
        print self.__class__.__name__,'.update_object'
        self.anagrafica.update_object(obj)
        self.dati_commerciali.update_object(obj)

    def reset_fields(self):
        print self.__class__.__name__,'.reset_fields'        
        self.anagrafica.reset_fields()
        self.dati_commerciali.reset_fields()
    
    def reset_focus(self):
        self.anagrafica.reset_focus()
    
    def after_save(self):
        pass
    
    def after_delete(self):
        self.update()
    
    def on_page_focus(self):
        print self.__class__.__name__,'.on_page_focus'
        print '''Questo metodo deve essere rimosso. Non viene piu invocato'''
        self.dati_commerciali.fill_combobox()
    
    def init_notebook(self):
        self.__class__.__name__,'.init_notebook'
        self.anagrafica = p1 = panel.AnagraficaPanel(self)
        self.dati_commerciali = p2 = DatiCommercialiPanel(self)
        #self.p0.MoveBeforeInTabOrder(self.fnb)
        #self.fnb.MoveBeforeInTabOrder(self.p0)
        self.notebook.AddPage(p1,' Anagrafica ')
        self.notebook.AddPage(p2,' Rapporti Commerciali ',False)
    
    def is_anagrafica_clienti(self):
        return self.TIPO=='C'


#===============================================================================
# ClientiPage
#===============================================================================
class ClientiPage(AnagraficaBasePage):
    TIPO='C'
    TITLE='Clienti'
    def __init__(self,*args,**kwargs):        
        AnagraficaBasePage.__init__(self,*args,**kwargs)
    
    def init_page_list(self):
        self.set_list_columns(co.Column('Codice',70,'ID'),
                              co.Column('Ragione Sociale',450,'ragione_sociale'),
                              co.Column('Partita IVA',100,'piva'),
                              co.Column('Listino',100,'get_listino_fmt'),
                              )
        
        #self.Show()
#commentato perche' per ora non metto il tab Ordini    
#    def init_notebook(self):
#        self.__class__.__name__,'init_notebook'
#        super(ClientiPage,self).init_notebook()
#        self.notebook.AddPage(wx.Panel(self),' Ordini ',False)
    
   
#===============================================================================
# FornitoriPage
#===============================================================================
class FornitoriPage(AnagraficaBasePage):
    TIPO='F'
    TITLE='Fornitori'
    def __init__(self,*args,**kwargs):
        AnagraficaBasePage.__init__(self,*args,**kwargs)
    
    def init_page_list(self):
        self.set_list_columns(co.Column('Codice',70,'ID'),
                              co.Column('Ragione Sociale',450,'ragione_sociale'),
                              co.Column('Partita IVA',100,'piva'),
                              )                
#come per i clienti
#    def init_notebook(self):
#        self.__class__.__name__,'init_notebook'
#        super(FornitoriPage,self).init_notebook()
#        self.notebook.AddPage(wx.Panel(self),'Prodotti',False) 
   